package com.yww.player

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.yww.player.databinding.ItemVideoListBinding
import java.util.ArrayList
import kotlin.coroutines.coroutineContext

class VideoListAdapter : RecyclerView.Adapter<VideoListAdapter.VideoViewHolder>() {
    var context: Context? = null
    var mData = mutableListOf<VideoModel>()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VideoViewHolder {
        context = parent.context
        return VideoViewHolder(
            DataBindingUtil.inflate(
                LayoutInflater.from(parent.context),
                R.layout.item_video_list,
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: VideoViewHolder, position: Int) {
        holder.inflate.bean = mData[position]
        holder.inflate.root.setOnClickListener {
            val intent = Intent(context, PlayVideoActivity::class.java)
            intent.putExtra("url", mData[position].path)
            context?.startActivity(intent)
        }
    }

    override fun getItemCount(): Int {
        return mData.size
    }

    @SuppressLint("NotifyDataSetChanged")
    fun update(it: ArrayList<VideoModel>?) {
        mData.clear()
        if (it != null) {
            mData.addAll(it)
        }
        notifyDataSetChanged()
    }

    class VideoViewHolder(var inflate: ItemVideoListBinding) :
        RecyclerView.ViewHolder(inflate.root) {

    }


}